package game.mapviewer.popups.battle 
{
	import com.greensock.TweenLite;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import game.map.Army;
	import game.map.Unit;
	import game.map.UnitTypes;
	import game.mapviewer.popups.ArmyOrdersPopup;
	/**
	 * ...
	 * @author rnk
	 */
	public class ArmyView extends Sprite
	{
		
		private var spriteHolder:Sprite;
		private var units:Array;
		private var loyalty:int = 0;
		private var bannersSprite:Sprite;
		
		public function ArmyView(loyalty:int,units:Array) 
		{
			spriteHolder = new Sprite();
			addChild(spriteHolder);
			this.units = units.slice();
			this.loyalty = loyalty;
			Refresh();
			AddBanner(loyalty);
		}
		
		public function AddUnits(newUnits:Array):void
		{
			units = units.concat(newUnits);
			Refresh();
		}
		
		public function AddBanner(playerUid:int):void
		{
			var banners:Array = [banner1, banner2, banner3];
			if (playerUid > 0 && playerUid <= banners.length)
			{
				var bannerClass:Class = banners[playerUid - 1] as Class;
				var banner:MovieClip = new bannerClass();
				banner.x = bannersSprite.width;
				bannersSprite.addChild(banner);
			}
		}
		
		public function GetPower(isAttacking:Boolean):int
		{
			var result:int = 0;
			for each (var unit:Unit in units) 
			{
				var paramName:String = isAttacking?"attack":"defend";
				result += UnitTypes.UNIT_PARAMS[unit.type][paramName];
			}
			return result;
		}
		
		public function Refresh():void
		{
			while (spriteHolder.numChildren) spriteHolder.removeChildAt(0);
			
			//loyalty banner
			if (!bannersSprite)
				bannersSprite = new Sprite();
			spriteHolder.addChild(bannersSprite);
			
			//units
			if (units.length > 0)
			{
				var unitTypes:Object = {};
				for (i = 0; i < units.length; i++)
				{
					var unit:Unit = units[i];
					var iconClass:Class;
					
					switch (unit.type) 
					{
						case UnitTypes.FOOTMAN:
							iconClass = footman;
						break;
						case UnitTypes.KNIGHT:
							iconClass = knight;
						break;
						case UnitTypes.ARCHER:
							iconClass = archer2;
						break;
						case UnitTypes.LANDSKNEHT:
							iconClass = landsknecht;
						break;
					}
					
					var unitIcon:MovieClip = new iconClass();
					if (!unitTypes[unit.type]) unitTypes[unit.type] = [];
					unitTypes[unit.type].push(unitIcon);
				}
				
				var rowOfUnits:Array = [];
				var maximumColumnLength:int = 0;
				var magicDx:Number = 10;
				var magicDy:Number = 8;
				var dx:Number = 0;
				
				for each (var typeList:Array in unitTypes) 
				{
					
					maximumColumnLength = Math.max(maximumColumnLength, typeList.length);
					var columnOfUnits:Array = [];
					var bounds:Rectangle;
					for (var i:int = 0; i < typeList.length; i++)
					{
						
						unitIcon = typeList[i];
						
						bounds = unitIcon.getBounds(unitIcon);
						
						unitIcon.x = dx - bounds.x;
						
						
						if (i > 0)
						{
							unitIcon.x += magicDx*i;
							unitIcon.y += magicDy*i;
						}
						
						columnOfUnits.push(unitIcon);
					}
					rowOfUnits.push(columnOfUnits);
					dx += bounds.width;
				}
				
				rowOfUnits.sortOn(['length']);
				
				/*rowOfUnits.sort(
					function (a:Array, b:Array):Number
					{
						var aPrice:Number = a.length;
						var bPrice:Number = b.length;
						
						if (aPrice > bPrice) 
						{
							return 1;
						} 
						else 
						if (aPrice < bPrice) 
						{
							return -1;
						} 
						else  
						{
							//aPrice == bPrice
							return 0;
						}
						
					}
				);*/
				
				for (var j:int = 0; j < maximumColumnLength; j++)
				{
					for (var k:int = 0; k < rowOfUnits.length; k++)
					{
						if (rowOfUnits[k].length>j)
						spriteHolder.addChild(rowOfUnits[k][j]);
					}
				}
				
				spriteHolder.x = -dx / 2;
				
				if (bannersSprite)
					bannersSprite.x = -spriteHolder.x - 15;
			}
		}
		

	}

}